package com.block.datapush.form.item;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.enums.BaseTypeInfoEnum;
import com.block.common.service.LogFactory;
import com.block.datapush.form.enums.PushResultEnum;
import com.block.datapush.form.item.util.XinDianDaiUtil;
import com.block.datapush.form.service.IFormPushHandle;
import com.block.datapush.model.dto.FormPushResultDTO;
import com.block.db.entity.FormInfo;
import com.block.db.entity.FormInfoHis;
import com.block.db.entity.MemberExtInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 新点贷
 */
@Service
public class XinDianDaiPushHandleService implements IFormPushHandle {

    private static final Logger log = LogFactory.getPushLog();

    /**
     * /api/Infos/userExist
     *
     * @param formInfo
     * @param memberExtInfo
     * @return
     */
    @Override
    public boolean queryMatchResult(FormInfo formInfo, MemberExtInfo memberExtInfo) throws Exception {

        String queryUrl = formInfo.getQueryUrl();

        String mobileMd5 = SecureUtil.md5(memberExtInfo.getMobile()).toLowerCase();

        Long time = System.currentTimeMillis();



        String signString = formInfo.getApiCode()+mobileMd5+time+formInfo.getApiSecret();
        String sign = SecureUtil.md5(signString);



        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("code", formInfo.getApiCode());
        paramMap.put("phone", mobileMd5);
        paramMap.put("idnum", memberExtInfo.getIdCard());
        paramMap.put("name", memberExtInfo.getUsername());
        paramMap.put("age", memberExtInfo.getAge());
        paramMap.put("gender", memberExtInfo.getSex() == 1 ? "男" : "女");
        paramMap.put("working_city", memberExtInfo.getCity());
        paramMap.put("loan_amount", BaseTypeInfoEnum.LoanAmount.getEntryByValue(memberExtInfo.getLoanAmount()).getAmount());
        paramMap.put("gjj", XinDianDaiUtil.getGjj(memberExtInfo.getGjj()));
        paramMap.put("shebao", XinDianDaiUtil.getShebao(memberExtInfo.getSocialInsurance()));
        paramMap.put("credit", "无");
        paramMap.put("insurance", XinDianDaiUtil.getBaodan(memberExtInfo.getInsurance()));
        paramMap.put("house", XinDianDaiUtil.getHouse(memberExtInfo.getHouse()));
        paramMap.put("car", XinDianDaiUtil.getCar(memberExtInfo.getCar()));
        paramMap.put("huabei", XinDianDaiUtil.getHuabei(memberExtInfo.getHuabei()));
        paramMap.put("baitiao", XinDianDaiUtil.getBaitiao(memberExtInfo.getBaitiao()));
        paramMap.put("license", "无");
        paramMap.put("zhima", XinDianDaiUtil.getZhima(memberExtInfo.getZhimaScore()));
        paramMap.put("credit_record", XinDianDaiUtil.getYuqi(memberExtInfo.getYuqi()));
        paramMap.put("time", time);
        paramMap.put("sign", sign);


        log.info("【新点贷】撞库开始【queryUrl】{}【paramMap】{}",
                queryUrl, JSON.toJSONString(paramMap));
        String result = HttpUtil.createPost(queryUrl)
                .header("Content-Type", "application/json")
                .body(JSON.toJSONString(paramMap))
                .execute().body();
        log.info("【新点贷】撞库结束【result】{}", result);

        JSONObject resultObject = JSON.parseObject(result);
        String code = resultObject.getString("code");
        String msg = resultObject.getString("msg");

        if (StrUtil.equals("200", String.valueOf(code))) {
            return true;
        }
        return false;
    }

    /**
     * /api/Infos/receive
     */
    @Override
    public FormPushResultDTO pushMemberInfo(FormInfo formInfo, MemberExtInfo memberExtInfo, String orderNo, JSONObject jsonObject) throws Exception {

        FormPushResultDTO pushResultDTO = new FormPushResultDTO();
        pushResultDTO.setOrderNo(orderNo);

        String queryUrl = formInfo.getRequestUrl();

        String phone = memberExtInfo.getMobile();
        Long time = System.currentTimeMillis();

        String signString = formInfo.getApiCode()+phone+time+formInfo.getApiSecret();
        String sign = SecureUtil.md5(signString);

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("code", formInfo.getApiCode());
        paramMap.put("phone", phone);
        paramMap.put("idnum", memberExtInfo.getIdCard());
        paramMap.put("name", memberExtInfo.getUsername());
        paramMap.put("age", memberExtInfo.getAge());
        paramMap.put("gender", memberExtInfo.getSex() == 1 ? "男" : "女");
        paramMap.put("working_city", memberExtInfo.getCity());
        paramMap.put("loan_amount", BaseTypeInfoEnum.LoanAmount.getEntryByValue(memberExtInfo.getLoanAmount()).getAmount());
        paramMap.put("gjj", XinDianDaiUtil.getGjj(memberExtInfo.getGjj()));
        paramMap.put("shebao", XinDianDaiUtil.getShebao(memberExtInfo.getSocialInsurance()));
        paramMap.put("credit", "无");
        paramMap.put("insurance", XinDianDaiUtil.getBaodan(memberExtInfo.getInsurance()));
        paramMap.put("house", XinDianDaiUtil.getHouse(memberExtInfo.getHouse()));
        paramMap.put("car", XinDianDaiUtil.getCar(memberExtInfo.getCar()));
        paramMap.put("huabei", XinDianDaiUtil.getHuabei(memberExtInfo.getHuabei()));
        paramMap.put("baitiao", XinDianDaiUtil.getBaitiao(memberExtInfo.getBaitiao()));
        paramMap.put("license", "无");
        paramMap.put("zhima", XinDianDaiUtil.getZhima(memberExtInfo.getZhimaScore()));
        paramMap.put("credit_record", XinDianDaiUtil.getYuqi(memberExtInfo.getYuqi()));
        paramMap.put("time", time);
        paramMap.put("sign", sign);


        log.info("【新点贷】导单开始【queryUrl】{}【paramMap】{}", queryUrl, JSON.toJSONString(paramMap));
        String result = HttpUtil.createPost(queryUrl)
                .header("Content-Type", "application/json")
                .body(JSON.toJSONString(paramMap))
                .execute().body();
        log.info("【新点贷】导单结束【result】{}", result);

        JSONObject resultObject = JSON.parseObject(result);
        String code = resultObject.getString("code");
        String msg = resultObject.getString("msg");


        pushResultDTO.setResult(result);
        pushResultDTO.setCode(String.valueOf(code));
        pushResultDTO.setMessage(msg);
        pushResultDTO.setParamContent(JSON.toJSONString(paramMap));

        if (StrUtil.equals("200", String.valueOf(code))) {
            pushResultDTO.setSuccess(true);
            pushResultDTO.setPushResult(PushResultEnum.SUCCESS.getType());
        } else {
            pushResultDTO.setPushResult(PushResultEnum.FAILED.getType());
            pushResultDTO.setSuccess(false);
        }
        return pushResultDTO;

    }


    @Override
    public void afterPushHandle(FormInfoHis formInfoHis) {

    }

}
